// MessageBox.cs
//

using System;
using System.Collections.Generic;
using jQueryApi;
using System.Html;
using System.Diagnostics;

namespace TuringMachine
{
   public enum CloseResult { ok = 1, cancel = 2, none = 0 };
   public abstract class MessageBoxBase
   {
      static int counter = 0;
      protected jQueryObject m_msgBox = null;
      protected TMButton m_okButton = null;
      protected TMButton m_cancelButton = null;
      jQueryObject m_bgHider = null;
      public event DialogEventHandler OnClose;
      bool m_bShowing = false;
      bool m_bShowingProcess = false;
      bool m_bCloseAfterShow = false;
      protected int m_PosX;
      protected int m_PosY;

      public MessageBoxBase(jQueryObject parent)
      {
         string suffix = counter + IdFactory.GetId();
         string saveloaddlgId = "dialog" + suffix;
         parent.Append("<div id=\"" + saveloaddlgId + "\"></div>");
         m_msgBox = jQuery.Select("#" + saveloaddlgId);
         m_msgBox.CSS("border", "2px solid black");
         m_msgBox.CSS("width", "800px");
         m_msgBox.CSS("background-color", "#d0d0d0");
         m_msgBox.CSS("position", "absolute");
         m_msgBox.CSS("text-align", "center");
         m_msgBox.CSS("z-index", "150");
         m_msgBox.CSS("display", "none");

         m_okButton = new TMButton(m_msgBox, "Ok", false);
         m_cancelButton = new TMButton(m_msgBox, "Cancel", false);


         string backgroundId = "bgHider" + suffix;
         parent.Append("<div id=\"" + backgroundId + "\"></div>");
         m_bgHider = jQuery.Select("#" + backgroundId);
         m_bgHider.CSS("display", "none");
         m_bgHider.CSS("position", "absolute");
         m_bgHider.CSS("opacity", "0.5");
         m_bgHider.CSS("left", "0px");
         m_bgHider.CSS("top", "0px");
         m_bgHider.CSS("z-index", "130");
         m_bgHider.CSS("background-color", "#000000");

         m_okButton.OnClick += new ButtonEventHandler(OkButton_Click);
         m_cancelButton.OnClick += new ButtonEventHandler(CancelButton_Click);

         m_PosX = m_msgBox.Position().Left + parent.Position().Left;
         m_PosY = m_msgBox.Position().Top + parent.Position().Top;
      }

      protected virtual void CancelButton_Click(TMButton sender)
      {
         Close(CloseResult.cancel);
      }

      protected virtual void OkButton_Click(TMButton sender)
      {
         Close(CloseResult.ok);
      }

      public virtual void Show()
      {
         if (!m_bShowing && !m_bShowingProcess)
         {
            m_bShowingProcess = true;
            m_bCloseAfterShow = false;
            m_msgBox.Show().CSS("z-index", "200").CSS("opacity", "0");
            m_bgHider.CSS("width", (Document.DocumentElement.ClientWidth - 20) + "px");
            m_bgHider.CSS("height", (jQuery.Window.GetHeight() - 20) + "px");

            PlaceElements();

            m_okButton.pPosY = m_msgBox.GetHeight() - 60;
            m_okButton.pPosX = (m_msgBox.GetWidth()) / 2 - 170;

            m_cancelButton.pPosY = m_msgBox.GetHeight() - 60;
            m_cancelButton.pPosX = m_msgBox.GetWidth() / 2 + 20;

            m_bgHider.FadeIn(200, delegate
            {
               m_msgBox.CSS("top", Math.Max(0, ((jQuery.Window.GetHeight() - m_msgBox.GetOuterHeight()) / 2) +
                                                  jQuery.Window.GetScrollTop()) + "px");
               m_msgBox.CSS("left", Math.Max(0, ((jQuery.Window.GetWidth() - m_msgBox.GetOuterWidth()) / 2) +
                                                           jQuery.Window.GetScrollLeft()) + "px");
               m_msgBox.Hide().CSS("opacity", "").FadeIn(200, delegate
               {
                  m_bShowing = true;
                  m_bShowingProcess = false;
                  ShowCompleted();
               });
            });
         }
      }


      public void Close(CloseResult result)
      {
         if (m_bShowingProcess)
            m_bCloseAfterShow = true;
         else
         {
            m_msgBox.Hide();
            m_bgHider.FadeOut(300, delegate
            {
               m_bShowing = false;
               if (result != CloseResult.none)
               {
                  if (OnClose != null)
                     OnClose(this, result == CloseResult.ok);
               }
            });
         }
      }


      private void ShowCompleted()
      {
         if (m_bCloseAfterShow)
         {
            Close(CloseResult.none);
         }
         OnShown();
      }

      protected abstract void PlaceElements();
      public virtual void OnShown()
      {

      }

      public string pCancelText
      {
         get { return m_cancelButton.pText; }
         set { m_cancelButton.pText = value; }
      }

      public string pOkText
      {
         get { return m_okButton.pText; }
         set { m_okButton.pText = value; }
      }
   }

}
